//
//  MCDistributionCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/22.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCDistributionCellClosure = (UIViewController) -> ()
typealias MCDistributionCell_shareClosure = (Int) -> ()

class MCDistributionCell: UITableViewCell {
    
    var closure : MCDistributionCellClosure?
    var shareClosure : MCDistributionCell_shareClosure?

    
    var marketingType = 0 {
        didSet {
            
            if marketingType == 0 {
                shareButton.isHidden = false
            } else {
                shareButton.isHidden = true
            }
        }
    }
    
    
    var model = MCDistributionModel() {
        didSet {
            nameLabel.text = model.title
            timeLabel.text = model.createTime
            activityTimeLabel.text = "活动时间:" + model.startTime.MCString_prefix(index: 10) + " ~ " + model.endTime.MCString_prefix(index: 10)
            pickUpTimeLabel.text = "提货时间:" + model.pickUpStartTime.MCString_prefix(index: 10) + " ~ " + model.pickUpEndTime.MCString_prefix(index: 10)

            alreadyCheckLabel.text = "已经核销订单:" + model.hasWriteOff.stringValue
            notCheckLabel.text = "未核销订单:" + model.notWriteOff.stringValue
           
            let community = model.community
            let province = community["province"] as? String ?? ""
            let city = community["city"] as? String ?? ""
            let area = community["area"] as? String ?? ""
            let address = community["address"] as? String ?? ""
            let communityName = community["communityName"] as? String ?? ""
             addressButton.setTitle(" " + communityName + " " + province + city + area + address, for:.normal)
            
            goodsNameLabel.text = model.allGoodsName
        }
    }
    
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        self.backgroundColor = UIColor.MCGray_light
        
        self.addSubview(timeLabel)
        timeLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(self)
            make.size.equalTo(CGSize.init(width: 140, height: 24))
            make.top.equalTo(13)
        }
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(50, 15, 0, 15))
        }
        
        
        bgView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(15)
            make.height.equalTo(22)
        }

        bgView.addSubview(activityTimeLabel)
        activityTimeLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(nameLabel)
            make.right.equalTo(-15)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.height.equalTo(17)
        }
        
        bgView.addSubview(pickUpTimeLabel)
        pickUpTimeLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(nameLabel)
            make.right.equalTo(-15)
            make.top.equalTo(activityTimeLabel.snp.bottom).offset(2)
            make.height.equalTo(17)
        }
        
        // 底部
        bgView.addSubview(lineTwoView)
        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(bgView)
            make.height.equalTo(1)
            make.bottom.equalTo(bgView).offset(-40)
        }
        
        bgView.addSubview(addressButton)
        addressButton.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(nameLabel)
            make.top.equalTo(lineTwoView.snp.bottom)
            make.bottom.equalTo(bgView)
        }

        
        bgView.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(bgView)
            make.height.equalTo(1)
            make.bottom.equalTo(lineTwoView.snp.top).offset(-47)
        }
        
        bgView.addSubview(orderButton)
        orderButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(nameLabel)
            make.top.equalTo(lineOneView.snp.bottom).offset(11)
            make.size.equalTo(CGSize.init(width: 70, height: 25))
        }
        
        bgView.addSubview(activityButton)
        activityButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(orderButton.snp.right).offset(18)
            make.centerY.equalTo(orderButton)
            make.size.equalTo(CGSize.init(width: 70, height: 25))
        }
        
        bgView.addSubview(shareButton)
        shareButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(activityButton.snp.right).offset(18)
            make.centerY.equalTo(orderButton)
            make.size.equalTo(CGSize.init(width: 70, height: 25))
        }

        
        //
        bgView.addSubview(alreadyCheckLabel)
        alreadyCheckLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(lineOneView.snp.top)
            make.height.equalTo(40)
        }
        
        bgView.addSubview(notCheckLabel)
        notCheckLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(nameLabel)
            make.bottom.equalTo(lineOneView.snp.top)
            make.height.equalTo(40)
        }


        bgView.addSubview(goodsNameLabel)
        goodsNameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(nameLabel)
            make.top.equalTo(pickUpTimeLabel.snp.bottom).offset(10)
            make.bottom.equalTo(alreadyCheckLabel.snp.top)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func buttonClicked(sender:UIButton) {
        let tag = sender.tag
        
        switch tag {
        case 1000:
            let vc = MCOrderPageViewController()
            vc.activityLogId = model.activityLogId.intValue
            closure?(vc)
        case 1001:
            let vc = MCActivityStatisticsViewController()
            vc.activityName = model.title
            vc.activityLogId = model.activityLogId.intValue
            vc.activityType = model.activityType.intValue
            closure?(vc)
        default:
            print("分享")
            shareClosure?(model.activityLogId.intValue)
        }
    }
    
    lazy var timeLabel: MCLabel = {
        let label = MCLabel()
        label.backgroundColor = MCColor_16(colorStr: "d8d8d8")
        label.font = MCFont(13)
        label.layer.masksToBounds = true
        label.layer.cornerRadius = 3
        label.textAlignment = NSTextAlignment.center
        return label
    }()

    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        return view
    }()
    

    
    lazy var nameLabel: MCLabel = {
        let label = MCLabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        return label
    }()
    
    lazy var activityTimeLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    lazy var pickUpTimeLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()

    lazy var goodsNameLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(13)
        label.backgroundColor = UIColor.MCGray_light
        label.textColor = UIColor.MCWords
        label.numberOfLines = 0
        return label
    }()

    lazy var alreadyCheckLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCMain
        return label
    }()

    lazy var notCheckLabel: MCLabel = {
        let label = MCLabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCMain
        return label
    }()
    
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var orderButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(12)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.setTitle("订单明细", for:.normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 13
        button.layer.borderColor = UIColor.MCGray_middle.cgColor
        button.layer.borderWidth = 1
        button.tag = 1000
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var activityButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(12)
        button.setTitleColor(UIColor.MCWords, for: .normal)
        button.setTitle("活动统计", for:.normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 13
        button.layer.borderColor = UIColor.MCGray_middle.cgColor
        button.layer.borderWidth = 1
        button.tag = 1001
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        return button
    }()

    lazy var shareButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(12)
        button.setTitleColor(UIColor.white, for: .normal)
        button.backgroundColor = UIColor.MCMain
        button.setTitle("活动分享", for:.normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 13
        button.tag = 1002
        button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
        return button
    }()

    
    lazy var addressButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.setTitleColor(UIColor.MCGray_middle, for: .normal)
        button.setImage(#imageLiteral(resourceName: "Reuse_location"), for: .normal)
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        button.isHighlighted = false
        return button
    }()
}
